package com.zhu.cloudspring.service;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.zhu.cloudspring.Repository.SmsLogRepository;
import com.zhu.cloudspring.entity.Order;
import com.zhu.cloudspring.entity.SmsLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class SmsService {

    @Autowired
    private SmsLogRepository smsLogRepository;

    @Value("${aliyun.sms.accessKey}")
    private String accessKeyId;

    @Value("${aliyun.sms.accessKeySecret}")
    private String accessKeySecret;

    @Value("${aliyun.sms.signName}")
    private String signName;

    @Value("${aliyun.sms.templateCode}")
    private String templateCode;
  @Value("${aliyun.sms.domain}")
    private String domain;
  @Value("${aliyun.sms.regionId}")
    private String regionId;

    public void sendSms(Order order, String consignee) {
        try {
            // 初始化acsClient,暂不支持region化
            DefaultProfile profile = DefaultProfile.getProfile(regionId,accessKeyId, accessKeySecret);
            IAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest();
            request.setMethod(MethodType.POST);
            request.setDomain(domain);
            request.setVersion("2017-05-25");
            request.setAction("SendSms");
            request.putQueryParameter("RegionId", regionId);
            request.putQueryParameter("PhoneNumbers", order.getPhoneNumber()); //目标手机号
            request.putQueryParameter("SignName", signName); //签名名称
            request.putQueryParameter("TemplateCode", templateCode); //短信模板code
            request.putQueryParameter("TemplateParam", "{\"consignee\":\"" + consignee + "\"}");//模板中变量替换

            CommonResponse response = client.getCommonResponse(request);
            String data = response.getData();
            // hint 此处可能会抛出异常，注意catch
            System.out.println("发送短信后的响应结果："+data);
            if (StringUtils.contains(data, "\"Message\":\"OK\"")) {
                // 请求成功
                SmsLog smsLog = new SmsLog();
                smsLog.setOrderId(order.getId());
                smsLog.setPhoneNumber(order.getPhoneNumber());
                smsLog.setMessage(consignee + "，创建订单成功。");
                smsLog.setSentAt(new Date());
                smsLogRepository.save(smsLog);
                System.out.println("SMS sent successfully to " + order.getPhoneNumber());
            } else {
                System.out.println("Failed to send SMS to " + order.getPhoneNumber() + ". Error code: " + data);
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
